package com.wl.shixun1.mymvc;

public class MyInt implements Num {

    private int f;

    public MyInt() {
    }

    public MyInt(int f) {
        this.f = f;
    }

    public int getF() {
        return f;
    }

    public void setF(int f) {
        this.f = f;
    }

    @Override
    public Num add(Num x) {
        if (x instanceof MyInt) {
            // 整数与整数相加
            MyInt result = new MyInt();
            MyInt xx = (MyInt) x;
            result.f = f + xx.f;
            return result;
        } else if (x instanceof MyDecimal) {
            // 整数与小数相加，转换为小数运算
            MyDecimal fx = new MyDecimal(f);
            return fx.add(x);
        } else {
            // 整数与分数相加，转换为分数运算
            Fraction fx = new Fraction(f, 1);
            return fx.add(x);
        }
    }
    
    @Override
    public Num sub(Num x) {
        if (x instanceof MyInt) {
            // 整数与整数相减
            MyInt result = new MyInt();
            MyInt xx = (MyInt) x;
            result.f = f - xx.f;
            return result;
        } else if (x instanceof MyDecimal) {
            // 整数与小数相减，转换为小数运算
            MyDecimal fx = new MyDecimal(f);
            return fx.sub(x);
        } else {
            // 整数与分数相减，转换为分数运算
            Fraction fx = new Fraction(f, 1);
            return fx.sub(x);
        }
    }
    
    @Override
    public Num mul(Num x) {
        if (x instanceof MyInt) {
            // 整数与整数相乘
            MyInt result = new MyInt();
            MyInt xx = (MyInt) x;
            result.f = f * xx.f;
            return result;
        } else if (x instanceof MyDecimal) {
            // 整数与小数相乘，转换为小数运算
            MyDecimal fx = new MyDecimal(f);
            return fx.mul(x);
        } else {
            // 整数与分数相乘，转换为分数运算
            Fraction fx = new Fraction(f, 1);
            return fx.mul(x);
        }
    }
    
    @Override
    public Num div(Num x) {
        if (x instanceof MyInt) {
            // 整数除以整数
            MyInt xx = (MyInt) x;
            if (xx.f == 0) {
                throw new ArithmeticException("除数不能为零");
            }
            MyInt result = new MyInt();
            result.f = f / xx.f;
            return result;
        } else if (x instanceof MyDecimal) {
            // 整数除以小数，转换为小数运算
            MyDecimal fx = new MyDecimal(f);
            return fx.div(x);
        } else {
            // 整数除以分数，转换为分数运算
            Fraction fx = new Fraction(f, 1);
            return fx.div(x);
        }
    }

    @Override
    public String draw1() {
        return f + "";
    }
    
    // 实现三个数的表达式计算，根据运算符优先级进行计算
    @Override
    public Num calculateExpression(Num num2, Num num3, String op1, String op2) {
        // 先判断运算符优先级
        if ((op1.equals("mul") || op1.equals("div")) && (op2.equals("add") || op2.equals("sub"))) {
            // 如果第一个运算符是乘除，第二个是加减，则先计算第一个
            Num temp = this;
            switch (op1) {
                case "mul":
                    temp = this.mul(num2);
                    break;
                case "div":
                    temp = this.div(num2);
                    break;
                case "add":
                    temp = this.add(num2);
                    break;
                case "sub":
                    temp = this.sub(num2);
                    break;
            }
            
            // 然后计算第二个运算符
            switch (op2) {
                case "mul":
                    return temp.mul(num3);
                case "div":
                    return temp.div(num3);
                case "add":
                    return temp.add(num3);
                case "sub":
                    return temp.sub(num3);
            }
        } else if ((op2.equals("mul") || op2.equals("div")) && (op1.equals("add") || op1.equals("sub"))) {
            // 如果第二个运算符是乘除，第一个是加减，则先计算第二个
            Num temp = num2;
            switch (op2) {
                case "mul":
                    temp = num2.mul(num3);
                    break;
                case "div":
                    temp = num2.div(num3);
                    break;
                case "add":
                    temp = num2.add(num3);
                    break;
                case "sub":
                    temp = num2.sub(num3);
                    break;
            }
            
            // 然后计算第一个运算符
            switch (op1) {
                case "mul":
                    return this.mul(temp);
                case "div":
                    return this.div(temp);
                case "add":
                    return this.add(temp);
                case "sub":
                    return this.sub(temp);
            }
        } else {
            // 如果运算符优先级相同，则从左到右计算
            Num temp = this;
            switch (op1) {
                case "mul":
                    temp = this.mul(num2);
                    break;
                case "div":
                    temp = this.div(num2);
                    break;
                case "add":
                    temp = this.add(num2);
                    break;
                case "sub":
                    temp = this.sub(num2);
                    break;
            }
            
            // 然后计算第二个运算符
            switch (op2) {
                case "mul":
                    return temp.mul(num3);
                case "div":
                    return temp.div(num3);
                case "add":
                    return temp.add(num3);
                case "sub":
                    return temp.sub(num3);
            }
        }
        
        return null;
    }
}